﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Openlab.Tools.CodeGender.Funtions
{

    public class CreateEntityDAFunc : Function
    {
        string entityClass;
        string storedProcedureName;
        Column idColumn;
        List<Column> columns;

        public CreateEntityDAFunc(string entityClass, string storedProcedureName, List<Column> columns, Column idColumn)
            : base(
                string.Format("Create{0}", entityClass)
            , "static " + entityClass, null)
        {
            this.entityClass = entityClass;
            this.storedProcedureName = storedProcedureName;
            this.columns = columns;
            this.idColumn = idColumn;
            this.Parameters = new List<FunctionParameter>();
            this.Parameters.Add(new FunctionParameter(entityClass.ToFirstLower(), entityClass));
        }

        public override string Body
        {
            get
            {
                string paramEntity = entityClass.ToFirstLower();
                StringBuilder code = new StringBuilder();

                code.AppendLine("using (SqlConnection conn = GetSqlConnection())");
                code.AppendLine("{");
                if (idColumn.CSTypeName == "int" || idColumn.CSTypeName == "Guid")
                {
                    SqlDbType type = SqlDbType.Int;
                    if (idColumn.CSTypeName == "Guid")
                        type = SqlDbType.UniqueIdentifier;
                    code.AppendLineFormatWithTabs("SqlParameter sp{0} = new SqlParameter(\"@{0}\", SqlDbType.{1});", 1, idColumn.Name, type);
                    code.AppendLineFormatWithTabs("sp{0}.Direction = ParameterDirection.Output;", 1, idColumn.Name);
                    code.AppendLineFormatWithTabs("sp{0}.Value = {1}.{0};", 1, idColumn.Name, paramEntity);
                    code.AppendLine();
                }
                code.AppendLineFormatWithTabs("SqlHelper.ExecuteNonQuery(conn, \"{0}\",", 1, storedProcedureName);
                bool first = true;
                foreach (Column column in columns)
                {
                    code.Append("\t\t");
                    if (!first)
                        code.Append(",");
                    else
                        first = false;
                    if (column.Name == idColumn.Name
                        && (idColumn.CSTypeName == "int" || idColumn.CSTypeName == "Guid"))
                    {
                        code.AppendLineFormat("sp{0}", column.Name);
                    }
                    else
                    {
                        code.AppendLineFormat("new SqlParameter(\"@{0}\", {1}.{0})", column.Name, paramEntity);
                    }
                }
                code.AppendLineWithTabs(");", 1);
                if (idColumn.CSTypeName == "int" || idColumn.CSTypeName == "Guid")
                {
                    code.AppendLineFormatWithTabs("{0}.{1} = ({2})sp{1}.Value;", 1, paramEntity, idColumn.Name, idColumn.CSTypeName);
                }
                code.AppendLineFormatWithTabs("return {0};", 1, paramEntity);
                code.AppendLine("}");

                return code.ToString();
            }
        }
    }

}
